import {TypeCheck} from '../common.js'
import {Module, EventDispatcher} from "../framework.js"
import DataStorage from "../data-storage.js"
import PageEvent from "./page-event.js"
import PageProperty from "./page-property.js"
//import * as WidgetEvent from "../widget/widget-event.js"
import WidgetController from "../widget/widget-controller.js"
import PageTOC from "./page-toc.js"
import D from "../delegator.js"

/*
class _PageLayoutSwitcher {
    constructor() {
        this._ui = D(Layout.layout).QueryElement("#ID_main");
        this._arrElement = [D(this._ui).QueryElement("#ID_main_toc"), D.QueryElement("#ID_main_summary"), D.QueryElement("#ID_main_content")];
        this._contentShow = true;
    }

    ShowBlankContent() {
        if(this._contentShow) {
            const _fn = D(this._ui).CacheRestoreStyle("display", 'none');
            D.Prepend(Layout.CreateBlankPage());
            this._arrElement.forEach((v) => {
                D.Remove(v);
            });
            _fn();
            this._contentShow = false;
        }
    }

    ShowPageContent() {
        if(this._contentShow) return;
        const _fn = D(this._ui).CacheRestoreStyle("display", 'none');
        D.RemoveAllChildren();
        this._arrElement.forEach((v) => {
            D.Append(v);
        });
        _fn();
        this._contentShow = true;
    }
}
*/

export default class Page extends Module {
    constructor(applicationDispatcher) {
        super(applicationDispatcher);
        this._PID = undefined;
        this._disp = EventDispatcher();
        //this._ui = document.QueryElement("#ID_main");
        //this._switcher = new _PageLayoutSwitcher();
        this._property = new PageProperty(this._dispatcher);

        this._toc = new PageTOC(this._disp);
        this._widgetController = new WidgetController(this._disp);

        //this._switcher.ShowBlankContent();


        const _rl = DataStorage.RegistListener;
        _rl(DataStorage.EVT_DATASTORAGE_ARTICLE_FETCHED, event => {
            const _pContent = event.pageContent;
            this._PID = _pContent.SUID;
            const _pageConfig = DataStorage.GetPageProperty(this._PID);
            //this.sidebar.Select(_meta.SUID);
            //this._HandlePageLayout(_pageConfig);
            this._toc.Update(_pageConfig);
            this._property.Update(_pageConfig);
            this._widgetController.Update(_pContent);
            //this._switcher.ShowPageContent();
        })
        _rl(DataStorage.EVT_DATASTORAGE_META_MODIFIED, event => {
            const _pProperty = event.pageProperty;
            //this._HandlePageLayout(_pProperty);
            this._property.Update(_pProperty);
        })
        _rl(DataStorage.EVT_DATASTORAGE_META_ADDED, event => {
            DataStorage.FetchPageContent(event.pageProperty.SUID);
        })
        _rl(DataStorage.EVT_DATASTORAGE_META_DELETED, event => {
            //this._switcher.ShowBlankContent();
        })
    }

    /*
    _HandlePageLayout(pc){
        if (TypeCheck.IsObject(pc)) {
            D(this._ui);
            if (pc.toc) {
                D.AddStyle('grid-template-columns', '4fr 1fr');
            } else {
                D.AddStyle('grid-template-columns', 'auto 0px');
            }
            if (pc.property) {
            } else {
            }
        }
    }
    */
}

